package com.etime.shycourse.controller;

import com.alibaba.fastjson.JSONObject;
import com.etime.shy.common.BaseMessage;
import com.etime.shycourse.dto.Signature;
import com.etime.shycourse.services.interfaces.MediaNotifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Random;

/**
 * 获取腾讯云点播签名的数据接口
 */
@Api(tags = "获取腾讯云点播上传视频签名的数据接口")
@RestController
@RequestMapping("/signature")
public class SignatureController {

    @ApiOperation("获取腾讯云点播视频上传签名的数据接口")
    @GetMapping("/getUgcUploadSign")
    public BaseMessage getUgcUploadSign() {
        BaseMessage msg = new BaseMessage ();
        Signature sign = new Signature ();
        //AKIDGPVUO5a1IL9rYCqNjtrhWJ0fDh9Xo19E
        sign.setSecretId ("AKIDGPVUO5a1IL9rYCqNjtrhWJ0fDh9Xo19E");
        //42vqgOuCIBoCMHxObaKp56rNehU3NSLz
        sign.setSecretKey ("42vqgOuCIBoCMHxObaKp56rNehU3NSLz");
        sign.setCurrentTime (System.currentTimeMillis () / 1000);
        sign.setRandom (new Random ().nextInt (java.lang.Integer.MAX_VALUE));
        sign.setSignValidDuration (3600 * 24 * 2);
        try {
            String signature = sign.getUploadSignature ();
            System.out.println ("signature : " + signature);
            msg.setCode (1);
            msg.setMsg (signature);
        } catch (Exception e) {
            System.out.print ("获取签名失败");
            e.printStackTrace ();
            msg.setCode (0);
            msg.setMsg ("获取签名失败");
        }
        return msg;
    }
    @ApiOperation("获取腾讯云点播视频上传签名的数据接口（非mp4格式的视频调用此接口）")
    @GetMapping("/getUgcUploadSign2")
    public BaseMessage getUgcUploadSign2() {
        BaseMessage msg = new BaseMessage ();
        Signature sign = new Signature ();
        //AKIDGPVUO5a1IL9rYCqNjtrhWJ0fDh9Xo19E
        sign.setSecretId ("AKIDGPVUO5a1IL9rYCqNjtrhWJ0fDh9Xo19E");
        //42vqgOuCIBoCMHxObaKp56rNehU3NSLz
        sign.setSecretKey ("42vqgOuCIBoCMHxObaKp56rNehU3NSLz");
        sign.setCurrentTime (System.currentTimeMillis () / 1000);
        sign.setRandom (new Random ().nextInt (java.lang.Integer.MAX_VALUE));
        sign.setSignValidDuration (3600 * 24 * 2);
        try {
            String signature = sign.getUploadSignature2 ();
            System.out.println ("signature : " + signature);
            msg.setCode (1);
            msg.setMsg (signature);
        } catch (Exception e) {
            System.out.print ("获取签名失败");
            e.printStackTrace ();
            msg.setCode (0);
            msg.setMsg ("获取签名失败");
        }
        return msg;
    }

    @Autowired
    private MediaNotifyService mediaNotifyService;
    /**
     前端：
     1、在上传获取签名时，先判断文件类型，如果不是mp4格式的，则采用getUgcUploadSign2接口进行签名
     2、后端：1、回调接口中的内容写入到notify表中记录（以备在转码完成还没有提交课时的时候，手工操作使用）
     3、将回调内容的主要信息保存到notify中（新表：medianotify）
     4、更新课时表中的播放视频路径
     */
    @ApiOperation("腾讯云点播回调接口(不要手动调用)")
    @RequestMapping("/notify")
    public String notify(@RequestBody JSONObject object, HttpServletRequest request, HttpServletResponse response){
        return mediaNotifyService.medianotify(object);
    }
}
